//
//  ZJKEmoticonKeyBoardViewModel.swift
//  MicroBlog
//
//  Created by VAD on 16/4/27.
//  Copyright © 2016年 朱江坤. All rights reserved.
//

import UIKit

// 每一页有多少列
let ZJKEmoticonKetboardMaxCol = 7
// 每一页有多少行
let ZJKEmoticonKetboardMaxRow = 3
// 每一个页显示多少个表情按钮
let ZJKEmoticonKetboardPageEmticonCount = ZJKEmoticonKetboardMaxCol * ZJKEmoticonKetboardMaxRow - 1

class ZJKEmoticonKeyBoardViewModel: NSObject {
    
    static let sharedViewModel: ZJKEmoticonKeyBoardViewModel = ZJKEmoticonKeyBoardViewModel()
    
    // 表情的bundle
    private lazy var emoticonBundle: NSBundle = {
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        return NSBundle(path: path)!
    }()
    
    // 定义一个数组记录默认表情
    lazy var defaultEmoticons: [ZJKEmoticon] = {
        
     return self.emoticonsWithPath("default/info.plist")
        
    }()
    
    //emoji表情
    lazy var emojiEmoticons: [ZJKEmoticon] = {
        
        return self.emoticonsWithPath("emoji/info.plist")
    }()
    
    //浪小花
    lazy var lxhEmoticons: [ZJKEmoticon] = {
        
        return self.emoticonsWithPath("lxh/info.plist")
        
    }()
    
    
    private func emoticonPageArray(emoticons: [ZJKEmoticon]) -> [[ZJKEmoticon]] {
        
        //求出表情一共有多少页
        let pageCount = (emoticons.count - 1) / 20 + 1
        
        //定义返回数组
        var result: [[ZJKEmoticon]] = [[ZJKEmoticon]]()
        
        // 遍历截取每一页的内容
        for i in 0..<pageCount {
            // 从哪儿开始截取
            let loc = 20 * i
            // 截取的长度
            var len = 20
            
            // 判断是否越界
            if loc + len > emoticons.count {
                len = emoticons.count - loc
            }
            
            let childArray = (emoticons as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as! [ZJKEmoticon]
            result.append(childArray)
        }
        return result

    }
    
    
    private func emoticonsWithPath(filePath: String) -> [ZJKEmoticon]{
        
        // 1. 取到默认表情的info.plist路径
        let path = self.emoticonBundle.pathForResource(filePath, ofType: nil)!
        
        // 2. 读取字典数组
        let dicts = NSArray(contentsOfFile: path)!
        
        // 3. 初始化一个模型数组以保存模型
        var tempArray = [ZJKEmoticon]()
        
        // 4. 字典转模型
        for value in dicts {
            
            let model = ZJKEmoticon(dict: (value as! [String : AnyObject]))
            
            // 设置该表情对应的文件夹路径
            let folderName =  (filePath as NSString).stringByDeletingLastPathComponent
            
            model.folderName = folderName
            
            tempArray.append(model)
        }
        
        // 5. 返回
        return tempArray
        
    }
    

}
